﻿using Business.Base.Enums;
using Business.Base.Model;
using Business.CentrifugalPump.Perf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace Pumptester.ExperimentForms.CentrifugalPump.Perf
{
    public partial class CentrifugalPumpPerfViewForm : Pumptester.ExperimentForms.Base.BaseViewForm
    {
        public CentrifugalPumpPerfViewForm()
        {
            InitializeComponent();
        }

        public CentrifugalPumpPerfViewForm(Experiment experiment, BaseChartData chart ,Form preForm) : base(experiment,chart,preForm)
        {
            this.ChartDrawer = new CentrifugalPumpPerfChart();
            InitializeComponent();
            this.dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dataGridView.MergeColumnNames.Add("Column0");
            this.dataGridView.MergeColumnNames.Add("Column1");
            this.dataGridView.MergeColumnNames.Add("Column2");
            this.dataGridView.MergeColumnNames.Add("Column3");
            this.dataGridView.MergeColumnNames.Add("Column4");
            this.dataGridView.MergeColumnNames.Add("Column5");
            this.dataGridView.MergeColumnNames.Add("Column6");
            this.dataGridView.MergeColumnNames.Add("Column7");
            this.dataGridView.MergeColumnNames.Add("Column8");
            this.dataGridView.MergeColumnNames.Add("Column9");
            this.dataGridView.AddSpanHeader(0, 10, "转换前数据");
            this.dataGridView.MergeColumnNames.Add("Column10");
            this.dataGridView.MergeColumnNames.Add("Column11");
            this.dataGridView.MergeColumnNames.Add("Column12");
            this.dataGridView.MergeColumnNames.Add("Column13");
            this.dataGridView.MergeColumnNames.Add("Column14");
            this.dataGridView.MergeColumnNames.Add("Column15");
            this.dataGridView.AddSpanHeader(10, 6, "转换后数据");

        }



        protected override void updateExperimentInfo()
        {
            CentrifugalPumpPerfExperiment experiment = (CentrifugalPumpPerfExperiment)this.Experiment;
            //experiment.ExperimentCode = this.textBoxCode.Text;
            //experiment.PumpName = this.textBoxPumpName.Text;
            //experiment.PumpModel = this.textBoxProductModel.Text;
            //experiment.PumpCode = this.textBoxPumpCode.Text;
            //experiment.ExperimentOwner = this.textBoxExperimentOwner.Text;
            //experiment.Manufacturer = this.textBoxManufacturer.Text;
            experiment.PumpRatedFlow = double.Parse(this.textBoxRatedFlow.Text);
            experiment.PumpRatedHead = double.Parse(this.textBoxRatedHead.Text);
            experiment.PumpRatedPower = double.Parse(this.textBoxRatedPower.Text);
            experiment.MotorRatedSpeed = double.Parse(this.textBoxRatedSpeed.Text);
            //experiment.MotorRatedVoltage = (Voltage)int.Parse(this.textBoxRatedVoltage.Text);
            //experiment.MotorRatedCurrent = double.Parse(this.textBoxRatedCurrent.Text);
            experiment.PumpRatedEfficiency = double.Parse(this.textBoxRatedEff.Text);
            //experiment.MotorWindingConnection = this.textBoxWindingConnection.Text;
            //experiment.MotorWindingMaterial = this.textBoxWindingMaterial.Text;
            experiment.MotorRatedEfficiency = double.Parse(this.textBoxMotorEff.Text);
            //experiment.ImpellerDia = double.Parse(this.textBoxImpellerDia.Text)/1000.0;
            //experiment.DInlet = double.Parse(this.textBoxDInlet.Text)/1000.0;
            //experiment.DOutlet = double.Parse(this.textBoxDOutlet.Text)/1000.0;
            //experiment.ZInlet = double.Parse(this.textBoxZInLet.Text)/1000.0;
            // experiment.ZOutlet = double.Parse(this.textBoxZOutLet.Text)/1000.0;
            //experiment.Flowmeter = int.Parse(this.textBoxFlowmeter.Text);
            //experiment.TransformationRatio = double.Parse(this.textBoxCurrentTransRatio.Text);
            //experiment.Customer = this.textBoxCustomer.Text;
            experiment.ExperimentLevel = (FlowHeadLevel)int.Parse(this.textBoxLevel.Text.Substring(0, 1));
            this.ExperimentService.ReCaculateRecord(this.Experiment);
            this.freshDataView();
            

        }

        protected override void LoadExperimentInfo()
        {
            CentrifugalPumpPerfExperiment experiment = (CentrifugalPumpPerfExperiment)this.Experiment;
            this.textBoxCode.Text = experiment.ExperimentCode;
            this.textBoxPumpName.Text = experiment.PumpName;
            this.textBoxProductModel.Text = experiment.PumpModel;
            this.textBoxPumpCode.Text = experiment.PumpCode;
            this.textBoxExperimentOwner.Text = experiment.ExperimentOwner;
            this.textBoxManufacturer.Text = experiment.Manufacturer;

            this.textBoxRatedFlow.Text = experiment.PumpRatedFlow.ToString();
            this.textBoxRatedHead.Text = experiment.PumpRatedHead.ToString();
            this.textBoxRatedPower.Text = (experiment.PumpRatedPower ).ToString();

            this.textBoxRatedSpeed.Text = experiment.MotorRatedSpeed.ToString();
            this.textBoxRatedVoltage.Text = ((int)experiment.MotorRatedVoltage).ToString();
            this.textBoxRatedCurrent.Text = experiment.MotorRatedCurrent.ToString();
            this.textBoxRatedEff.Text = experiment.PumpRatedEfficiency.ToString();
            this.textBoxWindingConnection.Text = experiment.MotorWindingConnection;
            this.textBoxWindingMaterial.Text = experiment.MotorWindingMaterial;
            this.textBoxMotorEff.Text = experiment.MotorRatedEfficiency.ToString();
            this.textBoxImpellerDia.Text =  (experiment.ImpellerDia*1000).ToString();

            this.textBoxDInlet.Text =       (experiment.DInlet*1000).ToString();
            this.textBoxDOutlet.Text =      (experiment.DOutlet*1000).ToString();

            this.textBoxZInLet.Text = (experiment.ZInlet*1000).ToString();
            this.textBoxZOutLet.Text = (experiment.ZOutlet*1000).ToString();

            this.textBoxFlowmeter.Text = experiment.Flowmeter.ToString();
            this.textBoxCurrentTransRatio.Text = experiment.TransformationRatio.ToString();
            this.textBoxCustomer.Text = experiment.Customer;
            this.textBoxLevel.Text = ((int)experiment.ExperimentLevel)+"B";


            for (int i = 0; i < experiment.RecordList.Count; i++)
            {
                CentrifugalPumpPerfExperimentRecord record = (CentrifugalPumpPerfExperimentRecord)experiment.RecordList[i];
                DataGridViewRow row = (DataGridViewRow)this.dataGridView.RowTemplate.Clone();

                DataGridViewTextBoxCell idCell = new DataGridViewTextBoxCell();
                idCell.Value = i+1;
                idCell.Tag = false;


                DataGridViewTextBoxCell workFlowCell = new DataGridViewTextBoxCell();
                workFlowCell.Value = record.WorkingFlow.ToString("f6");
                workFlowCell.Tag = true;


                DataGridViewTextBoxCell presureInCell = new DataGridViewTextBoxCell();
                presureInCell.Value = record.PresureIn.ToString("f6");
                presureInCell.Tag = true;

                DataGridViewTextBoxCell presureOutCell = new DataGridViewTextBoxCell();
                presureOutCell.Value = record.PresureOut.ToString("f6");
                presureOutCell.Tag = true;

                DataGridViewTextBoxCell voltageCell = new DataGridViewTextBoxCell();
                voltageCell.Value = record.Voltage.ToString("f6");
                voltageCell.Tag = true;

                DataGridViewTextBoxCell electricCurrentCell = new DataGridViewTextBoxCell();
                electricCurrentCell.Value = record.ElectricCurrent.ToString("f6");
                electricCurrentCell.Tag = true;

                DataGridViewTextBoxCell workHeadCell = new DataGridViewTextBoxCell();
                workHeadCell.Value = record.WorkingHead.ToString("f6");
                workHeadCell.Tag = true;


                DataGridViewTextBoxCell powerCell = new DataGridViewTextBoxCell();
                powerCell.Value = (record.WorkingPower / 1000.0).ToString("f6");
                powerCell.Tag = true;

                DataGridViewTextBoxCell speedCell = new DataGridViewTextBoxCell();
                speedCell.Value = record.WorkingSpeed.ToString("f6");
                speedCell.Tag = true;

                DataGridViewTextBoxCell temperatureCell = new DataGridViewTextBoxCell();
                temperatureCell.Value = record.Temperature.ToString("f6");
                temperatureCell.Tag = true;


                DataGridViewTextBoxCell stpQCell = new DataGridViewTextBoxCell();
                stpQCell.Value = record.StpFlow.ToString("f6");

                stpQCell.Tag = false;


                DataGridViewTextBoxCell stpHeadCell = new DataGridViewTextBoxCell();
                stpHeadCell.Value = record.StpHead.ToString("f6");
                stpHeadCell.Tag = false;

                DataGridViewTextBoxCell stpShaftPowerCell = new DataGridViewTextBoxCell();
                stpShaftPowerCell.Value = record.StpShaftPower.ToString("f6");
                stpShaftPowerCell.Tag = false;


                DataGridViewTextBoxCell stpPumpEfficiencyCell = new DataGridViewTextBoxCell();
                stpPumpEfficiencyCell.Value = record.StpPumpEfficiency.ToString("f6");
                stpPumpEfficiencyCell.Tag = false;

                DataGridViewTextBoxCell stpGroupEfficiencyCell = new DataGridViewTextBoxCell();
                stpGroupEfficiencyCell.Value = record.StpGroupEfficiency.ToString("f6");
                stpGroupEfficiencyCell.Tag = false;

                DataGridViewTextBoxCell staticHeadCell = new DataGridViewTextBoxCell();
                staticHeadCell.Value = record.WorkingStaticHead.ToString("f6");
                staticHeadCell.Tag = false;


                DataGridViewTextBoxCell dynamicHeadHeadCell = new DataGridViewTextBoxCell();
                dynamicHeadHeadCell.Value = record.WorkingDynamicHead.ToString("f6");
                dynamicHeadHeadCell.Tag = false;

                DataGridViewTextBoxCell geoHeadHeadCell = new DataGridViewTextBoxCell();
                geoHeadHeadCell.Value = record.WorkingGeoHead.ToString("f6");
                geoHeadHeadCell.Tag = false;

                DataGridViewTextBoxCell frictionHeadCell = new DataGridViewTextBoxCell();
                frictionHeadCell.Value = record.WorkingFrictionHead.ToString("f6");
                frictionHeadCell.Tag = false;



                row.Cells.Add(idCell);
                row.Cells.Add(workFlowCell);
                row.Cells.Add(presureInCell);
                row.Cells.Add(presureOutCell);
                row.Cells.Add(voltageCell);
                row.Cells.Add(electricCurrentCell);
                row.Cells.Add(powerCell);
                row.Cells.Add(workHeadCell);
                row.Cells.Add(speedCell);
                row.Cells.Add(temperatureCell);
                row.Cells.Add(stpQCell);
                row.Cells.Add(stpHeadCell);
                row.Cells.Add(stpShaftPowerCell);
                row.Cells.Add(stpPumpEfficiencyCell);
                row.Cells.Add(stpGroupEfficiencyCell);
                row.Cells.Add(staticHeadCell);
                row.Cells.Add(dynamicHeadHeadCell);
                row.Cells.Add(geoHeadHeadCell);
                row.Cells.Add(frictionHeadCell);
                row.Tag = record;

                this.dataGridView.Rows.Add(row);


            }
        }

        private void freshDataView()
        {
            LoadExperimentInfo();
            ((CentrifugalPumpPerfChartData)this.ChartData).ShowHeadRange = true;
            this.ChartDrawer.DrawChart(this.ChartData, this.userChart);
        }



        private void CentrifugalPumpPerfViewForm_Load(object sender, EventArgs e)
        {
            freshDataView();
        }

        private void textBoxRatedFlow_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
